package com.idealighter.game.server.db.datadb.dao;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.idealighter.game.server.db.datadb.generate.domain.RoomCardPlayerRoundCountDomain;
import com.idealighter.game.server.db.datadb.generate.domain.RoomCardPlayerRoundCountDomainExample;
import com.idealighter.game.server.db.datadb.mapper.RoomCardPlayerRoundCountMapper;
import com.idealighter.utils.check.EmptyUtil;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Singleton
public class RoomCardPlayerRoundCountDao {
  private RoomCardPlayerRoundCountMapper mapper;

  @Inject
  public RoomCardPlayerRoundCountDao(RoomCardPlayerRoundCountMapper mapper) {
    this.mapper = mapper;
  }

  /**
   * @Title findPlayerRoundCount.
   * @Description 获取玩家局数统计
   * @date 2018年6月22日 上午11:28:24
   * @param playerId 玩家编号
   * @return .
   */
  public RoomCardPlayerRoundCountDomain findPlayerRoundCount(long playerId) {
    RoomCardPlayerRoundCountDomainExample example = new RoomCardPlayerRoundCountDomainExample();
    example.createCriteria().andPlayerIdEqualTo(playerId);
    example.setLimit(1);
    List<RoomCardPlayerRoundCountDomain> list = mapper.selectByExample(example);
    RoomCardPlayerRoundCountDomain result = null;
    if (EmptyUtil.listIsNotEmpty(list)) {
      result = list.get(0);
    }
    return result;
  }

  /**
   * @Title insertSelective.
   * @Description 插入数据
   * @date 2018年6月22日 上午11:37:20
   * @param record 记录
   * @return .
   */
  public int insertSelective(RoomCardPlayerRoundCountDomain record) {
    return mapper.insertSelective(record);
  }

  /**
   * @Title updatePlayerRoundCount.
   * @Description 更新玩家玩的局数
   * @date 2018年6月22日 上午11:59:12
   * @param playerId 玩家编号
   * @param record 记录
   * @return .
   */
  public int updatePlayerRoundCount(long playerId, RoomCardPlayerRoundCountDomain record) {
    RoomCardPlayerRoundCountDomainExample example = new RoomCardPlayerRoundCountDomainExample();
    example.createCriteria().andPlayerIdEqualTo(playerId);
    return mapper.updateByExampleSelective(record, example);
  }

  /**
   * @Title incRoundCount.
   * @Description 增加局数
   * @date 2018年6月22日 上午10:50:21
   * @param playerId 玩家编号
   * @param incNum 增加的局数
   * @return 影响的条数
   */
  public int incRoundCount(long playerId, int incNum) {
    int count = mapper.increasePlayerRoundCount(playerId, incNum);
    if (count == 0) {
      RoomCardPlayerRoundCountDomain record = new RoomCardPlayerRoundCountDomain();
      record.setCount(incNum);
      record.setPlayerId(playerId);
      count = mapper.insert(record);
    }
    return count;
  }

  /**
   * @Title findRoundCount.
   * @Description 获取玩家玩的局数
   * @date 2018年6月22日 上午10:53:11
   * @param playerIds 玩家编号
   * @return Map&lt;Long, Integer&gt; 玩家编号对应的局数
   */
  public Map<Long, Integer> findRoundCount(List<Long> playerIds) {
    RoomCardPlayerRoundCountDomainExample example = new RoomCardPlayerRoundCountDomainExample();
    example.createCriteria().andPlayerIdIn(playerIds);
    List<RoomCardPlayerRoundCountDomain> list = mapper.selectByExample(example);

    Map<Long, Integer> result = new HashMap<>();
    if (EmptyUtil.listIsNotEmpty(list)) {
      for (RoomCardPlayerRoundCountDomain item : list) {
        result.put(item.getPlayerId(), item.getCount());
      }
    }
    return result;
  }
}
